//
//  KKBaseVController.swift
//  MVVMSwfit
//
//  Created by CarShare on 2018/12/3.
//  Copyright © 2018年 CarShare. All rights reserved.
//

import UIKit

class KKBaseVController: UIViewController {
    
    var tableView : UITableView?
    
    let statusBarHeight = UIApplication.shared.statusBarFrame.height
    /* 不建议 懒加载 以其完完整写法来写
     1 闭包内部智能提示不友好，
     2 闭包内部出现 self 还要注意循环引用
     懒加载完整写法：
     lazy var name:UIView = {
         let nameV = UIView()
         return nameV
     }()
     */
    lazy var customNaviBar = KKBaseNaviBarView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.white
    
        self.configNaviBar()
        // iPhone 8  320,480(@3x 960,1440); iPhone XR 375,812(@3x 1125 × 2436)
        // 375,667(@3x 1125,1991)
        print(UIScreen.main.bounds,"====")
    }
    override var title: String?{
        didSet{
            customNaviBar.titleLab.text = title
        }
    }
}
extension KKBaseVController:UITableViewDelegate,UITableViewDataSource{

    func configTableView(tableFrame:CGRect = CGRect(x: 0, y: UIApplication.shared.statusBarFrame.height + 44, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height - UIApplication.shared.statusBarFrame.height - 44),cellClass : AnyClass = KKBaseTableCell.self,cellTypeIsClass:Bool = false) -> Void {
        
        tableView = UITableView(frame: tableFrame, style: .plain)
        tableView?.backgroundColor = .white
        tableView?.delegate = self
        tableView?.dataSource = self
        tableView?.separatorStyle = .none
        
        let cellClassStr = NSStringFromClass(cellClass).components(separatedBy: ".")[1]
        print("BaseVC 注册 cell = ：= ",cellClassStr,"isNib = ",cellTypeIsClass)
        
        if cellTypeIsClass {
            tableView?.register(cellClass, forCellReuseIdentifier: NSStringFromClass(cellClass))
        }else{
            tableView?.register(UINib.init(nibName: cellClassStr, bundle: Bundle.main), forCellReuseIdentifier: NSStringFromClass(cellClass))
        }
        
        view.addSubview(tableView!)
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
}
// MARK: - 配置自定义导航条
extension KKBaseVController{
    func configNaviBar() {

        customNaviBar = KKBaseNaviBarView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: (statusBarHeight + 44)))
        customNaviBar.backgroundColor = .white
//        customNaviBar.leftType = .backWord
        customNaviBar.leftBtn.addTarget(self, action: #selector(popVCClick), for: .touchUpInside)
        
        customNaviBar.layer.shadowOffset = CGSize(width: 0, height: -1)
        customNaviBar.layer.shadowOpacity = 0.5
        
        view.addSubview(customNaviBar)
    }
    
    @objc func popVCClick() {
        self.navigationController?.popViewController(animated: true)
    }
}
